/**
 * 
 */
package com.transer.web.action.signup;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.transer.console.ConsoleConstants;
import com.transer.constant.Constants;
import com.transer.job.AddUserJob;
import com.transer.utils.FileUtils;
import com.transer.utils.JobQueueOB;
import com.transer.utils.MD5;

/**
 * 
 * class defination are there
 * 
 * @author mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class TranserRegisteAction extends RegisteAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5515990599207197249L;
	// 证书
	private File certificate;
	// 文件的内容类型
	private String certificateContentType;
	// 上传文件名
	private String certificateFileName;

	@Override
	public String execute() throws Exception {
		super.execute();
		// 存在验证错误
		if (hasFieldErrors()) {
			return ActionSupport.INPUT;
		}
		// 如果上传的有证书.保存证书
		if (certificate != null) {
			// 后缀名
			String suffix = certificateFileName.substring(certificateFileName.indexOf("."));
			// 根路径
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			// 保存证书
			FileUtils.saveAs(certificate, rootPath + "/upload", user.getEmail() + suffix);
			// 证书路径
			user.setCertificate("/upload/" + user.getEmail() + suffix);
		}
		// 工作接受方
		user.setType(Constants.USER_TYPE_TRANSER);
		// 加密
		user.setPassword(MD5.getMD5(user.getPassword()));
		// 添加发送邮件
		JobQueueOB.getInstance().addJob(ConsoleConstants.QUEUE_INDEX_SAVE_USER, new AddUserJob(userDao, user));
		// 注册成功
		return ActionSupport.SUCCESS;
	}

	public boolean hasFieldErrors(String fieldName) {
		return getFieldErrors().get(fieldName) != null;
	}

	public File getCertificate() {
		return certificate;
	}

	public void setCertificate(File certificate) {
		this.certificate = certificate;
	}

	public String getCertificateContentType() {
		return certificateContentType;
	}

	public void setCertificateContentType(String certificateContentType) {
		this.certificateContentType = certificateContentType;
	}

	public String getCertificateFileName() {
		return certificateFileName;
	}

	public void setCertificateFileName(String certificateFileName) {
		this.certificateFileName = certificateFileName;
	}

}